package wolftrace_company.study_android_ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import wolftrace_company.study_android_ui.R;

/**
 * Created by SimpleTonOne on 2018/4/1.
 */

public class GetDataActivity extends AppCompatActivity {
    private EditText editText1;
    private EditText editText2;
    private Button button;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.getdata);
        init();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                editText1.setText("获取数据成功了");
            }
        });
    }
    public void init(){
        editText1 = findViewById(R.id.edit1);
        editText2 = findViewById(R.id.edit2);
        button = findViewById(R.id.btn_getInternet);
    }
    public void getData(){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder().url("http://192.168.0.108:8080/jsonService/getJson").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("msg","获取数据失败了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                   // String username = request.body().toString();
                    String json = response.body().string();

                    Log.d("msg",json);
                    Message message = new Message();
                    message.what = 1;
                    message.obj = json;
                    handler.sendMessage(message);

                }
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                String a = (String) msg.obj;
                try {
                    JSONObject jsonObject = new JSONObject(a);
                    String b = jsonObject.getString("username");
                    String c = jsonObject.getString("password");
                    editText1.setText(b);
                    editText2.setText(c);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

        }
    };

}
